package com.graduation.fq.student.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.graduation.fq.student.Args.EventArgs;
import com.graduation.fq.student.Args.ScoreEventArgs;
import com.graduation.fq.student.Logic.MsgLogic;
import com.graduation.fq.student.Logic.ScoreLogic;
import com.graduation.fq.student.R;
import com.graduation.fq.student.Utils.CommonUtils;
import com.graduation.fq.student.Utils.Const;
import com.graduation.fq.student.domain.ScoreVO;
import com.graduation.fq.student.interfaces.EventListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jifang
 * Time 2016/5/29 16:12.
 */
public class ScoreQueryActivity extends Activity {

    private List<ScoreVO> scores;

    private ScoreQueryAdapter adapter;

    private String[] rules = {"成绩清单", "排名前十成绩", "尚未通过成绩"};

    private int position = 0;

    private EventListener listener = new EventListener() {

        @Override
        public void onEvent(EventArgs args) {
            ScoreEventArgs result = (ScoreEventArgs) args;
            if (result.getStatus() == Const.HTTP_SUCCESS) {
                scores.clear();
                scores.addAll(result.getScores());
                adapter.notifyDataSetChanged();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.score_query_layout);
        ListView listView = (ListView) findViewById(R.id.score_query_list_view);
        initListView(listView);


        final TextView ruleTV = (TextView) findViewById(R.id.score_rule);
        ruleTV.setText(rules[position]);
        getScoreList(position);
        ruleTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                position = getPosition(position);
                ruleTV.setText(rules[position]);
                getScoreList(position);
            }
        });
    }

    private int getPosition(int position) {
        ++position;
        if (position == 3)
            position = 0;
        return position;
    }

    private void getScoreList(int position) {
        Integer uId = CommonUtils.getUserId(this);
        final ScoreLogic logic = ScoreLogic.getInstance();
        if (position == 0) {
            logic.scoreList(uId, listener);
        } else if (position == 1) {
            logic.scoreTopN(uId, 10, listener);
        } else {
            logic.scoreUnPassed(uId, listener);
        }
    }

    private void initListView(ListView listView) {
        scores = new ArrayList<>();
        adapter = new ScoreQueryAdapter(scores);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ScoreVO score = (ScoreVO) parent.getAdapter().getItem(position);
                showLeaveMessageDialog(score);
            }
        });
    }

    private void showLeaveMessageDialog(final ScoreVO score) {
        final EditText messageEdit = new EditText(this);
        AlertDialog dialog = new AlertDialog.Builder(this).setView(messageEdit)
                .setTitle(String.format("向%s留言", score.gettName()))
                .setNegativeButton("取消", null).setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                sendMessage(messageEdit, score.gettId());
                            }
                        }
                ).create();
        dialog.show();
    }

    private void sendMessage(EditText text, int t_id) {
        String message = text.getText().toString().trim();
        if (TextUtils.isEmpty(message)) {
            Toast.makeText(ScoreQueryActivity.this, "请输入留言内容", Toast.LENGTH_SHORT).show();
        } else {
            MsgLogic logic = MsgLogic.getInstance();
            logic.send(message, CommonUtils.getUserId(ScoreQueryActivity.this), t_id);
        }
    }

    private class ScoreQueryAdapter extends BaseAdapter {

        private List<ScoreVO> scores;

        public ScoreQueryAdapter(List<ScoreVO> scores) {
            this.scores = scores;
        }

        public List<ScoreVO> getScores() {
            return scores;
        }

        public void setScores(List<ScoreVO> scores) {
            this.scores = scores;
        }

        @Override
        public int getCount() {
            return scores.size();
        }

        @Override
        public Object getItem(int position) {
            return scores.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ScoreVO score = getScores().get(position);
            ScoreViewHolder holder;

            if (convertView == null) {
                holder = new ScoreViewHolder();

                convertView = getLayoutInflater().inflate(R.layout.layout_score_query_item, null);
                holder.course = (TextView) convertView.findViewById(R.id.score_item_course);
                holder.credit = (TextView) convertView.findViewById(R.id.score_item_credit);
                holder.number = (TextView) convertView.findViewById(R.id.score_item_number);
                holder.teacher = (TextView) convertView.findViewById(R.id.score_item_teacher);
                convertView.setTag(holder);
            } else {
                holder = (ScoreViewHolder) convertView.getTag();
            }

            holder.course.setText(score.getcName());
            holder.number.setText(score.getNumber().intValue() == 0 ? "尚未打分" : String.valueOf(score.getNumber()));
            holder.credit.setText(String.valueOf(score.getCredit()));
            holder.teacher.setText(score.gettName());
            return convertView;
        }
    }

    private class ScoreViewHolder {
        TextView course;
        TextView number;
        TextView credit;
        TextView teacher;
    }
}
